package ReflectTest;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/*
通过反射机制去调用方法
 */
public class ReflectMethodTest2 {
    public static void main(String[] args) {
        try {
            Class c = Class.forName("java.lang.String");
            Object o = c.newInstance();
            Method method = c.getDeclaredMethod("valueOf",char.class); //前者方法名，后者形参类型
            Object o1 = method.invoke(o, '你');
            System.out.println(o1);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }
}
